import React, {Component} from "react";
import {
	View,
	Text,
	NetInfo,
	Button,
} from "react-native";

export default class NetInfoExam extends Component {
	constructor(props) {
		super(props)
	}
	componentDidMount() {
		NetInfo.fetch().done((status) => {
			console.log('status:' + status);
		})
		NetInfo.addEventListener('change', this.handleConnectivityChange)
	}
	componentWillUnmount() {
		NetInfo.removeEventListener('change', this.handleConnectivityChange)
	}
	handleConnectivityChange(status) {
		console.log('status change:' + status);
	}
	handlePress() {
		NetInfo.isConnected.fetch().done((isConnected) => {
			console.log('First, is' + (isConnected ? 'online' : 'offline'));
		})
		//该方法仅适用于android
		// NetInfo.isConnectionExpensive((isConnectionExpensive) => {
		// 	console.log('Connection is:' + (isConnectionExpensive ? "Expensive" : "Not Expensive"));
		// })
		
	}
	render() {
		return (
			<View>
				<Button 
					title="判断设备是否联网"
					onPress={this.handlePress}
				/>
				<Text>判断设备联网状态</Text>
			</View>
		);
	}
}